/**
 * 
 */
package com.antilia.infomaresme.data.dataobjects;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.persistence.UniqueConstraint;

/**
 * 
 * Category to index object.
 * 
 * @author Ernesto
 *
 */
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames={"title","parent_fk"})})
public class Category extends DomainObject {

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue
	private Long id;
	
	@Transient
	private Integer depth;
	
	@Column()
	private String title;
	
	private String description;
	
	/**
	 * This is used to group categories (business, geographic); 
	 */
	private CategoryDimension dimension = CategoryDimension.business;
	
	private Boolean topLevel = true;
	
	@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.REFRESH})
	@JoinColumn(name="parent_fk")
    private Category parent;
	
	@Transient
	private Set<Category> children = new java.util.HashSet<Category>();
	
	/**
	 * The language user for translations.
	 */
	private Language language;
		
	/**
	 * 
	 */
	public Category() {
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getDepth() {
		if(depth == null) {
			int i = 0;
			Category parent = this;
			while (parent != null) {
				i++;
				parent= parent.getParent();
			}
			depth = i;
		}
		return depth;
	}
	
	@Override
	public String toString() {
		return title;
	}
	
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Category getParent() {
		return parent;
	}

	public void setParent(Category parent) {
		this.topLevel = (parent==null);
		this.parent = parent;
	}

	public Set<Category> getChildren() {
		return children;
	}

	public void setChildren(Set<Category> children) {
		this.children = children;
	}
	
	public void addCategory(Category category) {
		this.children.add(category);
		category.setParent(this);
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public List<Category> getParentChain() {
		List<Category> parents = new ArrayList<Category>();
		Category category = this.getParent();
		while(category != null) {
			parents.add(0, category);
			category = category.getParent();
		}
		return parents;
	}

	public Boolean getTopLevel() {
		return topLevel;
	}

	public CategoryDimension getDimension() {
		return dimension;
	}

	public void setDimension(CategoryDimension dimension) {
		this.dimension = dimension;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((parent == null) ? 0 : parent.hashCode());
		result = prime * result + ((title == null) ? 0 : title.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Category other = (Category) obj;
		if (parent == null) {
			if (other.parent != null)
				return false;
		} else if (!parent.equals(other.parent))
			return false;
		if (title == null) {
			if (other.title != null)
				return false;
		} else if (!title.equals(other.title))
			return false;
		return true;
	}

	public Language getLanguage() {
		return language;
	}

	public void setLanguage(Language language) {
		this.language = language;
	}

}
